﻿using FS.Common.DotNetBean;
using FS.Common.DotNetCode;
using FS.Common.DotNetUI;
using FS.Business;
using System;
using System.Collections;
using System.Data;

namespace FS.Web.BLL.Visa
{
    public partial class Visa_PostVisaForm : PageBase
    {
        private string _key;
        private visa_dao _visa = new visa_dao();
        protected void Page_Load(object sender, EventArgs e)
        {
            this._key = base.Request["key"];
            if (!base.IsPostBack)
            {                            
                initBase_City();                
                if (!string.IsNullOrEmpty(this._key))
                {
                    this.InitData();
                }
            }
        }

        //所属国家
        private void initBase_City()
        {
            DataTable dt = _visa.GetSelect_CityInfo();
            ControlBindHelper.BindHtmlSelect(dt, this.City_id, "city_name", "city_id", "");
        }        

        //签证类型
        private void GetSelect_CertType(string ls_city)
        {
            DataTable dt = _visa.GetSelect_CertTypeInfoByCityID(ls_city);
            ControlBindHelper.BindHtmlSelect(dt, this.Ref_CertType, "name", "id", "");
        }

        //供应商
        private void GetSelect_Supply(string ls_city,string ls_certtype)
        {

            DataTable dt = _visa.GetSelect_SupplyInfoByCityIDAndCertType(ls_city, ls_certtype);
            ControlBindHelper.BindHtmlSelect(dt, this.Supply_id, "supply_name", "supply_id", "");
        }   

        private void InitData()
        {
            Hashtable ht = _visa.GetPostVisaForm(this._key);
            if (ht.Count > 0 && ht != null)
            {
                string ls_city = ht["CITY_ID"].ToString();
                string ls_certtype = ht["REF_CERTTYPE"].ToString();
                string ls_supply = ht["SUPPLY_ID"].ToString();
                //显示过滤后的下拉框
                this.GetSelect_CertType(ls_city);
                this.GetSelect_Supply(ls_city, ls_certtype);
                //数据保存
                this.HD_certtype.Value = ls_certtype;
                this.HD_supply.Value = ls_supply;

                ControlBindHelper.SetWebControls(this.Page, ht);
            }
        }
        protected void Save_Click(object sender, EventArgs e)
        {
            Hashtable ht = new Hashtable();
            //ht = ControlBindHelper.GetWebControls(this.Page);
            string ls_userid = RequestSession.GetSessionUser().UserId.ToString();
            string ls_username = RequestSession.GetSessionUser().UserName.ToString();
            if (!string.IsNullOrEmpty(this._key))
            {
                ht["InfoDetail_ID"] = this._key;
                ht["ModifyDate"] = DateTime.Now;
                ht["ModifyUserId"] = ls_userid;
                ht["ModifyUserName"] = ls_username;
            }
            else
            {
                ht["CertCity_id"] = CommonHelper.GetGuid;
                ht["CreateUserId"] = ls_userid;
                ht["CreateUserName"] = ls_username;
                ht["CreateDate"] = DateTime.Now;
            }

            ht["City_id"] = this.City_id.Value;
            ht["Ref_CertType"] = this.HD_certtype.Value;
            ht["Supply_id"] = this.HD_supply.Value;
            ht["InfoDetail_Submitdate"] = this.Submitdate.Value;
            ht["InfoDetail_user"] = this.InfoDetail_user.Value;
            ht["InfoDetail_cardno"] = this.InfoDetail_cardno.Value;
            ht["InfoDetail_remark"] = this.InfoDetail_remark.Value;

            bool IsOk=true;
            if (this.HD_change.Value == "1")
            {
                //重新计算费用
                IsOk = _visa.UpdateInfoPrice(this._key, ls_userid, ls_username);
            }

            if (IsOk)
            {
                IsOk = DataFactory.Run().Submit_AddOrEdit("Visa_InfoDetail", "InfoDetail_ID", this._key, ht);
                if (IsOk)
                {
                    ShowMsgHelper.AlertMsg("操作成功！");
                }
                else
                {
                    ShowMsgHelper.Alert_Error("操作失败！");
                }
            }
            else
            {
                ShowMsgHelper.Alert_Error("签证费用重新计算失败，请联系管理员！");
            }
        }
    }
}